package com.xydtech.ecard.merchant.service.impl;/*
 *@title EcardUserServiceImpl
 *@description
 *@author lin
 *@version 1.0
 *@create 2023/8/2 9:44
 */

import com.xydtech.common.annotation.DataScope;
import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.merchant.domain.EcardUser;
import com.xydtech.ecard.merchant.mapper.EcardUserMapper;
import com.xydtech.ecard.merchant.service.IEcardUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EcardUserServiceImpl implements IEcardUserService {
    @Autowired
    private EcardUserMapper ecardUserMapper;

    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<EcardUser> selectEcardUserList(EcardUser ecardUser, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardUser.getPhonenumber())) {
                ecardUser.setPhonenumber(AESEncryptionUtils.encrypt(ecardUser.getPhonenumber(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(ecardUser.getCardnum())) {
                ecardUser.setCardnum(AESEncryptionUtils.encrypt(ecardUser.getCardnum(), salt));
            }
            List<EcardUser> ecardUserList = ecardUserMapper.selectEcardUserList(ecardUser);
            if (ecardUserList.size() > 0) {
                for (EcardUser user : ecardUserList) {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(user.getPhonenumber())) {
                        user.setPhonenumber(AESEncryptionUtils.decrypt(user.getPhonenumber(), salt));
                    }
                    if (!StringUtils.objIsNullOrEmpty(user.getCardnum())) {
                        user.setCardnum(AESEncryptionUtils.decrypt(user.getCardnum(), salt));
                    }
                }
            }
            return ecardUserList;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
